import React, {Component} from 'react';
import ReactDom from 'react-dom';
import { FormattedMessage, injectIntl } from 'react-intl';
import {actions} from "mirrorx";
import {FormControl, Button, Modal, Message} from "tinper-bee";
import Radio from 'bee-radio';
import moment from 'moment'
import DatePicker from "bee-datepicker";
import cloneDeep from 'lodash.clonedeep';
import FormError from 'components/FormError';
import RefComponent from 'components/RefComponent';
import FormList from 'components/FormList'

let {RangePicker} = DatePicker;

import './index.less'

const FormItem = FormList.Item;

class CreateLabel extends Component {
    constructor(props) {
        super(props);
        this.state = {
        }

    }
    
    resetChecked=(dataValue)=>{
        let data = cloneDeep(dataValue);
        data.forEach((item,index)=>{
            item._checked=false
            item.index=index+1,
            item.key=index+1+''
        })
        return data
    }
    
    //填充至指定行
    extendToAssign=(getFieldProps,delFlag)=>{
        if (delFlag) {
            let rowNumStart = Number(getFieldProps("rowNumStart").value);
            let rowNumEnd = Number(getFieldProps("rowNumEnd").value);
            let {selectData,rowData} = this.props;
            let data = cloneDeep(this.props.data)
            data = this.resetChecked(data);
            let index = selectData[0].index;
            if(!data[index-1].customerId&&!data[index-1].channelTypeId){
                actions.ActivityPlan.updateState({ rowData: rowData });
                this.props.closeCreateModal();
                return;
            }
            //不填默认填充到末行
            if(!rowNumStart||!rowNumEnd){
                rowNumStart=index;
                rowNumEnd=data.length;
            }
            if(rowNumStart>rowNumEnd){
                Message.create({ content: '开始行数必须小于等于截止行数！', color: 'warning' });
                return;
            }
            let customerId = data[index-1].customerId;
            let customerCode = data[index-1].customerCode;
            let customerName = data[index-1].customerName;
            let channelTypeId = data[index-1].channelTypeId;
            let channelTypeCode = data[index-1].channelTypeCode;
            let channelTypeName = data[index-1].channelTypeName;
            data.map(dataItem => {
                if(dataItem.index >= rowNumStart && dataItem.index <= rowNumEnd && dataItem.persistStatus != "fdel") {
                    dataItem.customerId = customerId;
                    dataItem.customerCode = customerCode;
                    dataItem.customerName = customerName;
                    dataItem.channelTypeId = channelTypeId;
                    dataItem.channelTypeCode = channelTypeCode;
                    dataItem.channelTypeName = channelTypeName;
                }
            })
            rowData["activityPlanDetails"] = data;
            actions.ActivityPlan.updateState({ rowData: rowData });
            this.props.closeCreateModal();
        }else{
            this.props.closeCreateModal();
        }   
    }

    render() {
        let {form,onCallback,showCreateLabelModal} = this.props;
        let { getFieldProps, getFieldError } = this.props.form;

        let self = this;
        return (
            <Modal
            show={showCreateLabelModal}
            onHide={this.CreateLabel} >
            <Modal.Header>
                <Modal.Title>填充至指定行（不填默认至末行）</Modal.Title>
            </Modal.Header>

            <Modal.Body>
                <FormItem 
                    required={true}
                    label={'开始行数'}
                >
                        <FormControl
                            key={`rowNumStart`}
                            {...getFieldProps(`rowNumStart`, {
                                rules: [
                                    {
                                        required: true,
                                        message: "请输入",
                                    }
                                ],
                            })
                        }
                        />

                </FormItem>
                <FormItem 
                    required={true}
                    label={'截止行数'}
                >
                        <FormControl
                            key={`rowNumEnd`}
                            {...getFieldProps(`rowNumEnd`, {
                                rules: [
                                    {
                                        required: true,
                                        message: "请输入",
                                    }
                                ],
                            })
                        }
                        />

                </FormItem>
            </Modal.Body>

            <Modal.Footer>
                <Button onClick={() => this.extendToAssign(getFieldProps,false)} style={{ marginRight: 10 }}><FormattedMessage id="js.Ref.close" defaultMessage="关闭" /></Button>
                <Button onClick={() => this.extendToAssign(getFieldProps,true)} ><FormattedMessage id="js.Ref.chose" defaultMessage="确定" /></Button>
            </Modal.Footer>
        </Modal>

        )
    }
}

export default FormList.createForm()(injectIntl(CreateLabel))
